package com.betterjr.modules.customer.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterDateUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.account.service.CustAccountService;
import com.betterjr.modules.customer.AbstractBaseMechCustTemDataHandlerAdapter;
import com.betterjr.modules.customer.dao.CustMechManagerMapper;
import com.betterjr.modules.customer.entity.CustMechManager;
import com.betterjr.modules.customer.entity.CustMechManagerTmp;
import com.betterjr.modules.document.service.CustFileItemService;
import com.betterjr.modules.temhandler.data.TemDataConstant;
import com.betterjr.modules.temhandler.entity.TemData;
import com.betterjr.modules.temhandler.entity.TemDataBuilder;
import com.betterjr.modules.temhandler.service.TemDataService;

/**
 * 高管信息管理
 * @author liuwlBaseService<CustMechManagerMapper, CustMechManager>
 *
 */
@Service
public class CustMechManagerService
        extends AbstractBaseMechCustTemDataHandlerAdapter<CustMechManagerMapper, CustMechManager> {

    @Resource
    private CustMechManagerTmpService managerTmpService;

    @Resource
    private CustFileItemService fileItemService;

    @Resource
    private CustAccountService accountService;

    @Autowired
    private TemDataService temDataService;

    /**
     * 查询高管列表
     * @param anCustNo
     * @return
     */
    public List<CustMechManager> queryCustMechManager(Long anCustNo) {
        BTAssert.notNull(anCustNo, "客户编号不允许为空！");

        return this.selectByProperty("custNo", anCustNo);
    }

    /**
     * 查询高管信息
     */
    public CustMechManager findCustMechManager(Long anId) {
        BTAssert.notNull(anId, "高管编号不允许为空！");

        CustMechManager manager = this.selectByPrimaryKey(anId);
        return manager;
    }

    /**
     * 添加高管信息
     */
    public CustMechManager addCustMechManager(CustMechManagerTmp anManagerTmp) {
        BTAssert.notNull(anManagerTmp, "高管流水信息不允许为空！");

        CustMechManager manager = new CustMechManager();
        manager.initAddValue(anManagerTmp);
        this.insert(manager);

        return manager;
    }

    /**
     * 保存高管信息
     */
    public CustMechManager saveCustMechManager(CustMechManager anManager, Long anId) {
        BTAssert.notNull(anId, "高管编号不允许为空！");
        BTAssert.notNull(anManager, "高管信息不允许为空！");

        final CustMechManager tempManager = this.selectByPrimaryKey(anId);
        BTAssert.notNull(tempManager, "对应的高管信息没有找到！");

        tempManager.initModifyValue(anManager);
        this.updateByPrimaryKeySelective(tempManager);
        return tempManager;
    }

    /**
     * 通过高管流水修改高管 
     */
    public CustMechManager saveCustMechManager(CustMechManagerTmp anManagerTmp) {
        BTAssert.notNull(anManagerTmp, "高管流水编号不允许为空！");

        final CustMechManager tempManager = this.selectByPrimaryKey(anManagerTmp.getRefId());
        BTAssert.notNull(tempManager, "对应的高管信息没有找到！");

        tempManager.initModifyValue(anManagerTmp);
        this.updateByPrimaryKeySelective(tempManager);
        return tempManager;
    }

    @SuppressWarnings("unchecked")
    @Override
    public TemData saveAddOrUpdateTemData(TemData anTemData) {
        // 插入校验
        Map<String, Object> map = QueryTermBuilder.newInstance().put("custNo", anTemData.getCustNo()).build();
        List<TemData> temDataList = temDataService.queryTemData(anTemData.getBusinTableType(), "1,2,3", map, "2", 1,
                10);
        BTAssert.isTrue(Collections3.isEmpty(temDataList), "已经有变更在申请过程中");
        return temDataService.saveAddTemData(anTemData);
    }

    public CustMechManager addCustMechManagerTmp(CustMechManager custMechManager, String anFileList) {

        BTAssert.notNull(custMechManager, "公司基本信息-流水信息 不能为空！");
        BTAssert.notNull(custMechManager.getCustNo(), "公司基本信息-流水信息 不能为空！");
        custMechManager.saveAddInitValue(UserUtils.getOperatorInfo());
        custMechManager.setBatchNo(fileItemService.updateCustFileItemInfo(anFileList, custMechManager.getBatchNo()));
        TemData anTemData = convertToTemData(custMechManager);
        saveAddOrUpdateTemData(anTemData);

        return custMechManager;
    }

    private TemData convertToTemData(CustMechManager custMechManager) {

        TemData anTemData = TemDataBuilder.newInstance().businTableData(JSONObject.toJSONString(custMechManager))
                .businTableId(custMechManager.getId() + "").businTableOperorg(custMechManager.getOperOrg())
                .businTableType(custMechManager.getClass().getName()).laster(TemDataConstant.TEMDATA_LASTER_YES)
                .custNo(custMechManager.getCustNo())
                .custName(accountService.selectByPrimaryKey(custMechManager.getCustNo()).getCustName())
                .regDate(BetterDateUtils.getNumDate()).regOperid(UserUtils.getOperatorInfo().getId())
                .regOpername(UserUtils.getOperatorInfo().getName()).regTime(BetterDateUtils.getNumTime())
                .businStatus(TemDataConstant.TEMDATA_BUSINSTATUS_TEMTABLEINSERT).build();

        return anTemData;
    }

}
